<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员等级ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/customer/custRank.class.php');

class ajax_customerRank {

	//入口
	public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1:
                    $this->showRankList();//会员等级列表页面
                    break;

                case 2:
                    $this->searchCustRank(); //会员等级信息查询
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('edit'))) {
                        returnJsonData('参数错误');
                    }
                    if($_REQUEST['act'] == 'edit'){

                        $this->editRank();//新增、修改会员等级

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }
	
    //显示查询结果
    private function showRankList(){

		global $G_PGLOB;

		//实例化会员类
		$custRank = new custRank();
		
		//搜索会员等级数据
		$result_search = $custRank->searchRank($_REQUEST);

		//页面输出数组
		$result_array = array(
				'page' => $_REQUEST['page'],
				'total' => $_REQUEST['count'],
				'rows' => array()
		);

		//将查询结果组装进页面输出数组中
		if ($result_search){

            $edit_permission = $G_PGLOB->has_permission(3004);

			foreach ($result_search as &$search_item){

                if ($edit_permission) {
                    $free = ($search_item['freightFree'] == 1) ? '是' : '否';
                    $result_array['rows'][] = array(
                        'id' => $search_item['rankID'],
                        'cell' => array(
                            '<a href="javascript:void(0);" onclick="source.add('.$search_item['rankID'].');">修改</a>&nbsp;&nbsp;',
                            $search_item['rankID'],
                            $search_item['rankName'],
                            number_format($search_item['needAmt'],2,'.',''),
                            $search_item['rate'],
                            $search_item['pointsRate'],
                            $free
                        )
                    );
                }else{
                    $free = ($search_item['freightFree'] == 1) ? '是' : '否';
                    $result_array['rows'][] = array(
                        'id' => $search_item['rankID'],
                        'cell' => array(
                            $search_item['rankID'],
                            $search_item['rankName'],
                            number_format($search_item['needAmt'],2,'.',''),
                            $search_item['rate'],
                            $search_item['pointsRate'],
                            $free
                        )
                    );
                }
            }
        }
        echo json_encode($result_array);
	}

    /**
     * 会员等级信息查询
     */
    private function searchCustRank(){

        if(!isset($_REQUEST['rankID']) || !quanli_is_int($_REQUEST['rankID'])) {
           returnJsonData('参数错误');
        };

        //实例化会员类
        $custRank = new custRank();

        //搜索会员等级数据
        $rank_array = $custRank->searchRank( $_REQUEST );

        returnJsonData('ok', 200, $rank_array[0]);

    }

    /**
     * 新增、编辑会员等级
     */
    private function editRank(){

		global $G_PGLOB;

		//实例化会员等级类
		$custRank = new custRank();

        $_REQUEST['operator'] =$G_PGLOB->currentUserID;
		$result_array = $custRank->editCustomerRank($_REQUEST);

        returnJsonData($result_array['message'], $result_array['code']);
		
	}

}